package org.oschina.api.user.menu.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.user.menu.model.UserMenu;
import org.oschina.api.user.menu.service.UserMenuRoleService;
import org.oschina.api.user.menu.service.UserMenuService;
import org.oschina.api.user.menu.vo.UserMenuVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/user/menu")
public class UserMenuController extends BaseController {

	@Autowired
	private UserMenuService menuService;
	@Autowired
	private UserMenuRoleService menuRoleService;
	
	/**
	 * 返回
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<UserMenuVo> findVo()
	{
		return menuService.findVo();
	}
	
	/**
	 * 根据角色返回
	 * @param role
	 * @return
	 */
	@Validate
	@RequestMapping(value="/role/{role}",method=RequestMethod.GET)
	public List<UserMenuVo> findVoByRole
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer role
	)
	{
		return menuService.findVoByRole(role);
	}
	
	/**
	 * 更新
	 * @param menu
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="text",validateValue=true,filter={TextOnlyFilter.class}),
			@ValidateField(name="parent",validateValue=true,min=0),
			@ValidateField(name="main"),
			@ValidateField(name="index",validateValue=true,min=1)
		}) UserMenu menu
	)
	{
		return menuService.update(menu);
	}
	
	/**
	 * 创建
	 * @param menu
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="text",validateValue=true,filter={TextOnlyFilter.class}),
			@ValidateField(name="parent",validateValue=true,min=0),
			@ValidateField(name="index",validateValue=true,min=1),
			@ValidateField(name="main")
		}) UserMenu menu
	)
	{
		return menuService.create(menu);
	}
	
	/**
	 * 更新
	 * @param role
	 * @param menuRoles
	 * @return
	 */
	@Validate
	@RequestMapping(value="/role/update",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) int role,
		int menuRoles[]
	)
	{
		return menuRoleService.update(role, menuRoles);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return menuService.delete(id);
	}
	
	/**
	 * 根据令牌返回
	 * @return
	 */
	@RequestMapping(value="/token")
	public List<UserMenuVo> findVoByToken()
	{
		return menuService.findVoByUser();
	}
}